package cn.pegasus.teamwork.util;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;

public class HSSFWorkUtil {
	
	      
	    /** 
	     * 导出excel 
	     * @param templateFile - excel模版名称 
	     * @param beans - 模版中填充的数据 
	     * @param os - 生成模版输出流 
	     * @throws FileNotFoundException 
	     */  
	    public static void exportExcelJxl(String templateFile,Map<String,Object> beans,OutputStream os) throws FileNotFoundException {  
	    	 XLSTransformer transformer = new XLSTransformer(); 
	         InputStream is=HSSFWorkUtil.class.getClassLoader().getResourceAsStream(templateFile);  
	         try {  
	            Workbook workbook=transformer.transformXLS(is,beans);  
	            workbook.write(os);  
	        } catch (Exception e) {  
	            throw new RuntimeException("导出excel错误!", e);
	        }   
	    }  
	    public static void main(String[] args) throws Exception {  
	        OutputStream os=new FileOutputStream("E:/new.xls");  
	        String templateFile="D:/githome/pegasus_carwash/pegasus_carwash_web/src/main/webapp/docs/dfd.xlsx";  
	        Map<String, Object> beans=new HashMap<String, Object>();  
	          
	        // fruits  
	        List<Map<String,String>> fruitList=new ArrayList<Map<String,String>>();  
	          
	        Map<String,String> fruit=null;  
	        fruit=new HashMap<String, String>();  
	        beans.put("name", "苹果");  
	        beans.put("price", "100");  
	        fruit=new HashMap<String, String>();  
	        fruit.put("name", "香蕉");  
	        fruit.put("price", "200");  
	        fruitList.add(fruit);  
	          
	        beans.put("list",fruitList);
	        exportExcelJxl(templateFile, beans, os);  
	    }  
}
